package com.itcast.contraller;

import com.itcast.pojo.Emp;
import com.itcast.pojo.EmpQueryParam;
import com.itcast.pojo.PageResult;
import com.itcast.pojo.Result;
import com.itcast.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;
//    @GetMapping("/emps")
//    public Result getPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
//                          @RequestParam(value = "pageSize",defaultValue ="10") Integer pageSize,
//                          @RequestParam(value = "name") String name,
//                          @RequestParam(value = "gender") Integer gender,
//                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
//                          @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
//                          ){
//        log.info("查询员工信息,参数:{},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
//        PageResult<Emp> pag= empService.selectByPageAndCount(page,pageSize,name,gender,begin,end);
//        return Result.success(pag);
//    }

    @GetMapping("/emps")
    public Result getPage(EmpQueryParam empQueryParam) {
        log.info("查询员工信息", empQueryParam);
        PageResult<Emp> pag = empService.selectByPageAndCount(empQueryParam);
        return Result.success(pag);
    }
    /*
    * 保存员工
    * */
    @PostMapping("/emps")
    public Result saveEmp(@RequestBody Emp emp) {
        log.info("保存员工信息", emp);
        empService.saveEmp(emp);
        return Result.success();
    }
//    @DeleteMapping("/emps")
//    public Result deleteEmp(Integer[] ids){
//        for(int id:ids){
//            log.info("删除员工信息,id:{}",id);
//        }
//        log.info("删除员工信息",ids);

    /// /        empService.deleteEmp(ids);
//        return Result.success();
//    }
    /*
     * 删除员工
     * */
    @DeleteMapping("/emps")
    public Result deleteEmp(@RequestParam("ids") List<Integer> ids) {
        for (Integer id : ids) {
            log.info("删除员工信息,id:{}", id);
        }
        log.info("删除员工信息", ids.toString());
        empService.deleteEmpByID(ids);
        return Result.success();
    }
    /*
     * 根据Id查询员工信息
     * */
    @GetMapping("/emps/{id}")
    public Result findEmpById(@PathVariable("id") Integer id){
        log.info("查询员工信息,id:{}",id);
        Emp emp= empService.findEmpById(id);
        return Result.success(emp);
    }
    /*
     * 根据Id查询员工信息
     * */
//    @GetMapping("/emps/{id}")
//    public Result findEmpById(Emp emp) {
//        log.info("查询员工信息,id:{}", emp.getId());
//        emp = empService.findEmpById(emp.getId());
//        return Result.success(emp);
//    }

    @PutMapping("/emps")
    public Result updateEmp(@RequestBody Emp emp) {
        log.info("修改员工信息", emp);
        empService.updateEmp(emp);
        return Result.success();
    }

    @GetMapping("/emps/list")
    public Result findAllEmp() {
        log.info("查询所有员工信息");
        List<Emp> empList = empService.findAllEmp();
        return Result.success(empList);
    }
}
